package com.example.springbootblog.common.config;

import com.example.springbootblog.common.interceptor.LoginInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 拦截器的拦截URL的路径
 * 1. 一定要继承 WebMvcConfigurer 这个类
 * 2. 然后重写添加拦截器的方法 和 重写 添加来源操作
 *
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource(name = "loginInterceptor")
    LoginInterceptor interceptor;

    /**
     * 将不需要拦截的路径和文件放在List中
     */
    List<String> exclude = Arrays.asList(
            "/**/*.html",
            "/css/**",
            "/js/**",
            "/pic/**",
            "/*.jpg",
            "/*.png",
            "/user/login",
            "/user/register",
            "/captcha/**"
    );

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor)                    // 添加拦截器
                .addPathPatterns("/user/**","/blog/**")         // 添加拦截url 路径
                .excludePathPatterns(exclude);            // 添加不拦截的 url 路径
    }

}
